<template>
  <div>
    <el-button type="primary" @click="goToEditPage">新增贷款产品</el-button>
    <el-table
      :data="list"
      class="ymd-table"
    >
      <el-table-column
        prop="name"
        label="产品名称"
      >
      </el-table-column>
      <el-table-column
        label="logo"
      >
        <template scope="scope">
          <img :class="$style.logo" :src="scope.row.logo" alt="">
        </template>
      </el-table-column>
      <el-table-column
        prop="applyNum"
        label="申请人数"
      >
      </el-table-column>
      <el-table-column
        prop="rate"
        label="利率"
      >
      </el-table-column>
      <el-table-column
        prop="loanRange"
        label="贷款范围"
      >
      </el-table-column>
      <el-table-column
        prop="succRate"
        label="成功率"
      >
      </el-table-column>
      <el-table-column
        prop="fastTime"
        width="120"
        label="最快放款时间"
      >
      </el-table-column>
      <el-table-column
        prop="loanLimit"
        label="贷款期限"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
      >
        <template scope="scope">
          <el-button
            size="small"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="currentPageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import * as apis from 'services/products'

export default {
  name: 'LoanProduct',
  computed: {
    ...mapState({
      list: state => state.products.list,
      total: state => state.products.total,
    }),
  },
  data() {
    return {
      currentPage: 1,
      currentPageSize: 10,
    }
  },
  created() {
    this.$store.dispatch('getProducts')
  },
  methods: {
    goToEditPage() {
      this.$router.push('/loanProductEdit')
    },
    handleEdit(index, row) {
      this.$router.push({
        path: 'loanProductEdit',
        query: { id: row.id },
      })
    },
    handleSizeChange(pageSize) {
      this.currentPageSize = pageSize
      this.$store.dispatch('getProducts', {
        pageSize,
        page: this.currentPage,
      })
    },
    handleCurrentChange(page) {
      this.currentPage = page
      this.$store.dispatch('getProducts', {
        pageSize: this.currentPageSize,
        page,
      })
    },
    async handleDelete(index, row) {
      const isDel = await this.$confirm(`此操作将删除该${row.name}数据, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
      if (isDel) {
        await apis.delProductById(row.id)
        this.$store.dispatch('getProducts')
      }
    },
  },
}
</script>

<style module>
.logo {
  display: inline-block;
  width: 50px;
  height: 50px;
  margin: 12px 0;
  vertical-align: middle;
}
</style>
